package com.ukao.pad.base;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;

import com.ukao.pad.R;
import com.ukao.pad.listener.OnConfirmClickListener;
import com.ukao.pad.retrofit.ApiClient;
import com.ukao.pad.retrofit.ApiStores;
import com.ukao.pad.utils.SystemUIUtils;

import org.greenrobot.eventbus.EventBus;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Created by ukaodev on 2017/6/2.
 */

public class BaseDialogFragment extends DialogFragment{
    protected Activity activity;
    protected  Resources resources;
    public  static  final   String ARG_PARAM1 = "param1";
    public  static  final   String ARG_PARAM2 = "param2";
    public  static  final   String ARG_PARAM3= "param3";
    public   static  final  String ARG_PARAM4= "param4";
    public   static  final  String ARG_PARAM5= "param5";
    public OnConfirmClickListener mOnConfirmClickListener;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(STYLE_NORMAL, R.style.Dialog_FullScreen);
        resources = getActivity().getResources();
        activity = getActivity();
        if (useEventBus()) {
            EventBus.getDefault().register(this);
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    public Resources getResource() {
        return resources;
    }



    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);


    }


    public   void   setOnConfirmClickListener(OnConfirmClickListener  mOnConfirmClickListener){
                this.mOnConfirmClickListener=mOnConfirmClickListener;
    }

    private CompositeSubscription mCompositeSubscription;

    public ApiStores apiStores() {
        return ApiClient.retrofit().create(ApiStores.class);
    }

    public void addSubscription(Observable observable, Subscriber subscriber) {
        if (mCompositeSubscription == null) {
            mCompositeSubscription = new CompositeSubscription();
        }
        mCompositeSubscription.add(observable
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber));
    }

    public void onUnsubscribe() {

        //取消注册，以避免内存泄露
        if (mCompositeSubscription != null && mCompositeSubscription.hasSubscriptions())
            mCompositeSubscription.unsubscribe();
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        onUnsubscribe();
        if (useEventBus()) {
            EventBus.getDefault().unregister(this);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }


    public  void hideKeyboard(View mView ) {
        InputMethodManager imm = (InputMethodManager)
                getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(mView.getWindowToken(), 0);
    }
    public  void hideKeyboard( ) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
        if (imm != null) {
            imm.hideSoftInputFromWindow(activity.getWindow().getDecorView().getWindowToken(), 0);
        }
    }

    protected String getText(TextView txt) {
        String message = txt.getText().toString().trim();

        return message;
    }
    protected  void  initView(){}

    protected void initListener() {

    }
    protected  boolean useEventBus(){
        return false;
    }


}
